package com.expdatacloud.service.impl;

import com.expdatacloud.entity.IqcCheckItemDetails;
import com.expdatacloud.entity.IqcSamplingShow;
import com.expdatacloud.entity.SampleMgt;
import com.expdatacloud.entity.SampleMgtDetail;
import com.expdatacloud.mapper.IqcCheckItemDetailsMapper;
import com.expdatacloud.mapper.IqcSamplingShowMapper;
import com.expdatacloud.service.IqcSamplingShowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 检验明细表 服务实现类
 * </p>
 *
 * @author jie
 * @since 2019-11-14
 */
@Service
public class IqcSamplingShowServiceImpl implements IqcSamplingShowService {

    @Autowired
    private IqcSamplingShowMapper iqcSamplingShowMapper;
    @Autowired
    private IqcCheckItemDetailsMapper iqcCheckItemDetailsMapper;

    @Override
    public int add(IqcSamplingShow iqcSamplingShow) {
        return iqcSamplingShowMapper.add(iqcSamplingShow);
    }

    @Override
    public List<IqcSamplingShow> list(String receiptKey, String sku) {
        return iqcSamplingShowMapper.list(receiptKey, sku);
    }

    @Override
    public void updateRejectedNumber(IqcCheckItemDetails iqcCheckItemDetails) {
        double criticalDefect = 0;
        double seriousDefects = 0;
        double lightDefects = 0;
        List<IqcCheckItemDetails> iqcCheckItemDetailsList = iqcCheckItemDetailsMapper.list(null, iqcCheckItemDetails.getReceiptKey(), iqcCheckItemDetails.getSku());
        for (IqcCheckItemDetails checkItemDetails : iqcCheckItemDetailsList) {
            if (null != checkItemDetails.getCriticalDefect() && "NG".equalsIgnoreCase(checkItemDetails.getCriticalDefect().trim())) {
                criticalDefect += checkItemDetails.getNumberOfDefectiveSa();
            } else if (null != checkItemDetails.getSeriousDefects() && "NG".equalsIgnoreCase(checkItemDetails.getSeriousDefects().trim())) {
                seriousDefects += checkItemDetails.getNumberOfDefectiveSa();
            } else if (null != checkItemDetails.getLightDefects() && "NG".equalsIgnoreCase(checkItemDetails.getLightDefects().trim())) {
                lightDefects += checkItemDetails.getNumberOfDefectiveSa();
            }
        }
        //更新不良数
        IqcSamplingShow iqcSamplingShowCF = new IqcSamplingShow();
        iqcSamplingShowCF.setDefectLevels("致命缺陷");
        iqcSamplingShowCF.setRejectedNumber(criticalDefect);
        iqcSamplingShowCF.setReceiptKey(iqcCheckItemDetails.getReceiptKey());
        iqcSamplingShowCF.setSku(iqcCheckItemDetails.getSku());
        iqcSamplingShowMapper.updateRejectedNumber(iqcSamplingShowCF);

        IqcSamplingShow iqcSamplingShowSF = new IqcSamplingShow();
        iqcSamplingShowSF.setDefectLevels("严重缺陷");
        iqcSamplingShowSF.setRejectedNumber(seriousDefects);
        iqcSamplingShowSF.setReceiptKey(iqcCheckItemDetails.getReceiptKey());
        iqcSamplingShowSF.setSku(iqcCheckItemDetails.getSku());
        iqcSamplingShowMapper.updateRejectedNumber(iqcSamplingShowSF);

        IqcSamplingShow iqcSamplingShowLF = new IqcSamplingShow();
        iqcSamplingShowLF.setDefectLevels("轻微缺陷");
        iqcSamplingShowLF.setRejectedNumber(lightDefects);
        iqcSamplingShowLF.setReceiptKey(iqcCheckItemDetails.getReceiptKey());
        iqcSamplingShowLF.setSku(iqcCheckItemDetails.getSku());
        iqcSamplingShowMapper.updateRejectedNumber(iqcSamplingShowLF);
    }


    public void loopAssignment(SampleMgt sampleMgt, SampleMgtDetail sampleMgtDetail,
                               String checkScenarioName, String sku, String receiptKey) {
        for (int i = 0; i < 3; i++) {
            IqcSamplingShow iqcSamplingShow = new IqcSamplingShow();
            iqcSamplingShow.setSequenceNumber(i);
            iqcSamplingShow.setCheckScenarioName(checkScenarioName);
            iqcSamplingShow.setSampleQty(sampleMgtDetail.getSample_Qty());
            iqcSamplingShow.setSku(sku);
            iqcSamplingShow.setReceiptKey(receiptKey);
            if (i == 0) {
                iqcSamplingShow.setDefectLevels("致命缺陷");
                iqcSamplingShow.setAqlStandard(sampleMgt.getFatal_defect());
                iqcSamplingShow.setAcceptanceNumber(sampleMgtDetail.getFatal_Allow_Qty());
                iqcSamplingShow.setRejectionNumber(sampleMgtDetail.getFatal_Rejection_Qty());
            } else if (i == 1) {
                iqcSamplingShow.setDefectLevels("严重缺陷");
                iqcSamplingShow.setAqlStandard(sampleMgt.getSerious_defect());
                iqcSamplingShow.setAcceptanceNumber(sampleMgtDetail.getSerious_Allow_Qty());
                iqcSamplingShow.setRejectionNumber(sampleMgtDetail.getSerious_Rejection_Qty());
            } else {
                iqcSamplingShow.setDefectLevels("轻微缺陷");
                iqcSamplingShow.setAqlStandard(sampleMgt.getMinor_defects());
                iqcSamplingShow.setAcceptanceNumber(sampleMgtDetail.getMinor_Allow_Qty());
                iqcSamplingShow.setRejectionNumber(sampleMgtDetail.getMinor_Rejection_Qty());
            }
            iqcSamplingShowMapper.add(iqcSamplingShow);
        }
    }

    @Override
    public int delete(String receiptKey, String sku) {
        return iqcSamplingShowMapper.delete(receiptKey, sku);
    }

    @Override
    public void writeData(List<IqcSamplingShow> IqcSamplingShowList, String checkScenarioName,
                          String receiptKey, String sku, SampleMgt sampleMgt, SampleMgtDetail sampleMgtDetail) {
        if (IqcSamplingShowList.size() > 0 && IqcSamplingShowList.get(0).getCheckScenarioName() != null) {
            if (!IqcSamplingShowList.get(0).getCheckScenarioName().equals(checkScenarioName)) {
                delete(receiptKey, sku);
                //插入aql表
                loopAssignment(sampleMgt, sampleMgtDetail, checkScenarioName, sku, receiptKey);
            }
        } else {
            //插入aql表
            loopAssignment(sampleMgt, sampleMgtDetail, checkScenarioName, sku, receiptKey);
        }
    }
}
